import os
import base64
from github import Github

def encode_video(video_path):
    with open(video_path, "rb") as video_file:
        return base64.b64encode(video_file.read()).decode()

def main():
    # 设置 Gitee 访问令牌
    access_token = "您的_GITEE_访问令牌"
    repo_name = "您的仓库名称"
    
    # 初始化 Gitee API
    g = Github(access_token, base_url="https://gitee.com/api/v5")
    repo = g.get_user().get_repo(repo_name)
    
    # 视频文件夹路径
    video_folder = "视频文件夹路径"
    
    # 创建或更新 video.md 文件内容
    video_md_content = "# 视频列表\n\n"
    
    for filename in os.listdir(video_folder):
        if filename.endswith((".mp4", ".avi", ".mov")):  # 添加其他视频格式如果需要
            video_path = os.path.join(video_folder, filename)
            encoded_video = encode_video(video_path)
            
            # 上传视频文件到 Gitee
            repo.create_file(
                f"videos/{filename}",
                f"上传视频 {filename}",
                encoded_video,
                branch="main"
            )
            
            # 添加视频链接到 video.md
            video_md_content += f"- [{filename}](videos/{filename})\n"
    
    # 更新或创建 video.md 文件
    try:
        contents = repo.get_contents("video.md")
        repo.update_file(
            "video.md",
            "更新视频列表",
            video_md_content,
            contents.sha,
            branch="main"
        )
    except:
        repo.create_file(
            "video.md",
            "创建视频列表",
            video_md_content,
            branch="main"
        )

if __name__ == "__main__":
    main()